package com.trip_memories;

import java.util.UUID;

import signedUser.ChosedAlbum;
import signedUser.Signed;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import blogic.AccesBussineLogic;
import blogic.BusinessLogic;
import dbmodels.Album;
import dbtypes.PermisionAlbum;

public class NewAlbum extends Activity {

	private static final int DIALOG_ALERT = 5;
	private static final int DIALOG_ALERT1 = 10;
	BusinessLogic bL;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_album);
		bL = AccesBussineLogic.getBussinesLogic(this);
	}

	@SuppressWarnings({ "unused", "deprecation" })
	public void onClick(View view) {
		String name = ((EditText) findViewById(R.id.enter_name)).getText()
				.toString();
		int radioButtonId = ((RadioGroup) findViewById(R.id.radioGroup1))
				.getCheckedRadioButtonId();
		String privilege = getResources().getText(R.id.radioGroup1).toString();
		Album album = new Album();
		album.setName(name);
		album.setUserid(Signed.getId());
		album.setId(UUID.randomUUID());

		switch (radioButtonId) {
		case R.id.nothing:
			album.setPermision(PermisionAlbum.Nothing);
			break;
		case R.id.partial:
			album.setPermision(PermisionAlbum.Partial);
			break;
		case R.id.all:
			album.setPermision(PermisionAlbum.All);
			break;
		}

		if (album.isValid() && !bL.existAlbum(album.getName(), Signed.getId())) {

			bL.AddAlbum(album);
			ChosedAlbum.setId(album.getId());

			showDialog(DIALOG_ALERT1);
			
		} else {
			showDialog(DIALOG_ALERT);
		}
	}

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_ALERT: {
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.no_name_entered);
			builder.setCancelable(true);
			builder.setNegativeButton(android.R.string.cancel,
					new CancelOnClickListener());
			AlertDialog dialog = builder.create();
			dialog.show();
		}
			break;
		case DIALOG_ALERT1: {
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.album_inserted);
			builder.setCancelable(true);
			builder.setNegativeButton(android.R.string.ok,
					new OkOnClickListener());
			AlertDialog dialog = builder.create();
			dialog.show();
		}
			break;
		}
		return super.onCreateDialog(id);
	}

	private final class CancelOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
			finish();
		}
	}

	private final class OkOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
			finish();
		}
	}

}
